﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrmContacto.Contracts;
using CrmContacto.Contracts.Contracts;
using CrmContacto.Contracts.Contracts.Search;
using CrmContacto.WebApp.Code.InnerClasses;
using Web.CrmContacto.Code;
using Web.Framework;
using Web.Framework.Mapper;
using Web.Framework.Translate;
using Web.CrmContacto.Code.Security;

namespace CrmContacto.WebApp.App
{
    public partial class AccionDeMarketingComunicacionConfiguracionEdit : BasePage
    {
        public AccionDeMarketingComunicacionConfiguracion configuracionCurrent
        {
            get
            {
                return (AccionDeMarketingComunicacionConfiguracion)ViewState["AccionDeMarketingComunicacionConfiguracion"];
            }
            set
            {
                ViewState["AccionDeMarketingComunicacionConfiguracion"] = value;
            }
        }

        private List<LinkButton> actionButtons = new List<LinkButton>();
        LinkButton volverButton = new LinkButton();
        LinkButton guardarButton = new LinkButton();
        LinkButton eliminarButton = new LinkButton();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            
            guardarButton.ID = "BoCommandGuardar";
            guardarButton.Text = LocalizationProvider.Instance["Bo.Command.Guardar"];
            guardarButton.Click += new EventHandler(lnkGuardar_Click);
            actionButtons.Add(guardarButton);
            
            SetJavascriptForClientClickDirtyChech(volverButton);
            volverButton.ID = "BoCommandVolver";
            volverButton.CausesValidation = false;
            volverButton.Text = LocalizationProvider.Instance["Bo.Command.Volver"];
            volverButton.Click += new EventHandler(lnkCancelar_Click);
            actionButtons.Add(volverButton);
                        
            eliminarButton.ID = "BoCommandEliminar";
            eliminarButton.CausesValidation = false;
            eliminarButton.Text = LocalizationProvider.Instance["Bo.Command.Eliminar"];
            eliminarButton.Click += new EventHandler(eliminarButton_Click);
            actionButtons.Add(eliminarButton);

            base.LoadContextActions(actionButtons);
        }

        private void eliminarButton_Click(object sender, EventArgs e)
        {
            try
            {                
                WebServiceHelper.EliminarAccionMarketingConfiguracion(configuracionCurrent.Id);
                Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlMarketingConfiguracionList + string.Format("?{0}=1", MiscClass.QueryShowDeleted), true);
                //NuevaConfiguracionDeMarketing();
                //Bind();
                //this.masterPage.ShowMessage(LocalizationProvider.Instance["Bo.Message.RegistroEliminado"], WebMessageType.Notification);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }            
        }

        protected void Page_Load(object sender, EventArgs e)
        {            
            try
            {
                HasPermissionToSeeMe(CrmSecurity.MANAGE_MKT_COMMUNICATIONS_CONFIG);
                if (!this.IsPostBack)
                {
                    if (this.Request.QueryString["Id"] == null)
                    {
                        NuevaConfiguracionDeMarketing();
                    }
                    else
                    {
                        ConfiguracionAccionMarketingSearch search = new ConfiguracionAccionMarketingSearch();
                        search.EmpresaId = BaseWebPage.CompanyId;
                        search.Id = this.Request.QueryString["Id"];
                        configuracionCurrent = WebServiceHelper.GetAccionesMarketingConfiguracion(search).First();
                    }

                    Bind();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private void NuevaConfiguracionDeMarketing()
        {
            configuracionCurrent = new AccionDeMarketingComunicacionConfiguracion();
            configuracionCurrent.EmpresaId = BasePage.CompanyId;
        }

        protected override void ApplySecurity()
        {
            base.ApplySecurity();

            if (!String.IsNullOrEmpty(configuracionCurrent.Id))
            {
                SetJavascriptForDeleteRecord(eliminarButton, configuracionCurrent.Nombre);
            }

            this.eliminarButton.Visible = !String.IsNullOrEmpty(configuracionCurrent.Id);
            //this.lnkGuardar.Visible = HasPermissionFor(PlanProdSecurity.CAUSADESVIO_EDITAR);
        }

        private void Bind()
        {
            if (!String.IsNullOrEmpty(configuracionCurrent.Id))
            {                
                this.txtId.CssClass += "readonly";
                this.txtId.ReadOnly = true;
            }

            EntityMapper.Map(configuracionCurrent, this);
        }

        private void SetData()
        {
            EntityMapper.Map(this, configuracionCurrent);         
        }

        private void Save()
        {
            configuracionCurrent = WebServiceHelper.SaveAccionMarketingConfiguracion(configuracionCurrent);
        }

        private void Validar()
        {
            // TODO
        }

        protected void lnkGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                Validar();
                SetData();
                Save();
                Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlMarketingConfiguracionList + string.Format("?{0}=1", MiscClass.QueryShowSaveOk), true);
                //Bind();
                //this.masterPage.ShowMessage(LocalizationProvider.Instance["Bo.Messages.MensajeSaveExitoso"], WebMessageType.Notification);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkCancelar_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlMarketingConfiguracionList, true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
    }
}